Android 开发笔记

android开发过程中遇到的一些问题,应用场景,代码片段。有解决也有疑惑,一起探讨。。。…

1,PopupWindow消失的问题

在fragment中使用PopupWindow,点击它区域外的地方,让其自动隐藏。

1
2
3
4
5
6
7
8
9
10
11
12
13
private PopupWindow popupWindow;
private void showPopWindow() {
if(popupWindow == null){
View pop_view = activity.getLayoutInflater().inflate(R.layout.popwin_order_list, null);
popupWindow = new PopupWindow(pop_view, UIUtils.dip2px(context,100), RelativeLayout.LayoutParams.WRAP_CONTENT,true);
initRadioButton(pop_view);
}
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popupWindow.setOutsideTouchable(true);//该属性设置为true,则在点击屏幕的空白位置也会退出
popupWindow.setFocusable(true);
popupWindow.setTouchable(true);
popupWindow.showAsDropDown(toolbar.getTvRight2(), -80, 0);
}

2,FragmentTabHost切换其它fragment,后退crash

布局文件

1
2
3
4
5
6
7
8
9
10
11
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="@color/bottomTabBarColor">
<FrameLayout
android:id="@+id/fl_tab_wrap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.v4.app.FragmentTabHost>

代码初始化tabhost

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private void initTabHost() {
layoutInflater = LayoutInflater.from(this);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.fl_home_content);
int count = mFragmentArray.length;
for(int i = 0; i < count; i++){
TabHost.TabSpec tabSpec = mTabHost.newTabSpec(context.getString(mTextviewArray[i])).setIndicator(getTabItemView(i));
mTabHost.addTab(tabSpec, mFragmentArray[i], null);
mTabHost.getTabWidget().setShowDividers((LinearLayout.SHOW_DIVIDER_NONE));
}
}
private View getTabItemView(int index){
View view = layoutInflater.inflate(R.layout.home_tab_item, null);
ImageView imageView = (ImageView) view.findViewById(R.id.iv_tab_icon);
imageView.setImageResource(mImageViewArray[index]);
return view;
}

如果,当某个fragment中切换其他fragment(不是tab对应的mFragmentArray中),返回就会crash。原因是什么?

3,对话框问题

1
2
3
4
5
6
7
8
9
<!--自定义对话框-->
<style name="dialog1" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item><!--边框-->
<item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
<item name="android:windowIsTranslucent">true</item><!--半透明-->
<item name="android:windowNoTitle">true</item><!--无标题-->
<item name="android:windowBackground">@android:color/darker_gray</item><!--背景透明-->
<item name="android:backgroundDimEnabled">true</item><!--背景变暗-->
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public void showCommonDialog(final OnDialogBtnClickListener onDialogBtnClickListener) {
final Dialog dialog = new Dialog(context, R.style.dialog1);
//自定义布局文件 dialog.setContentView(R.layout.dialog_common_hint);
dialog.setCancelable(true);
btn_left = (Button) dialog.findViewById(R.id.btn_positive);
btnRight = (Button) dialog.findViewById(R.id.btn_negative);
TextView tv_description = (TextView)dialog.findViewById(R.id.tv_dialog_description);
tv_description.setText(description);
//左侧
btn_left.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onDialogBtnClickListener.onPositiveClick(v);
dialog.dismiss();
}
});
//右侧
btnRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onDialogBtnClickListener.onNegativeClick(v);
dialog.dismiss();
}
});
dialog.show();
}

dialog对应的布局,添加一个圆角背景

1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="@color/common_bg" />
</shape>

4,水平进度条

1、新建 myprogress_style.xml在drawable中

android:startColor=”@color/red” 为自定义开始颜色

为两头圆角的弧度,值越大越圆

为设置渐变色背景 angle 为角度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="8.0dip" />
<gradient android:startColor="#EEEEEE" android:endColor="#EEEEEE"
android:angle="270.0" />
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="0.0dip" />
<gradient android:startColor="@color/red" android:endColor="@color/red"
android:centerColor="@color/red" android:angle="270.0" />
</shape>
</clip>
</item>
</layer-list>

2、应用样式

1
2
3
4
5
6
<ProgressBar android:id="@+id/pic_ProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:visibility="gone"
android:layout_height="5dip"
android:progressDrawable="@drawable/myprogress_style">

5,splash首屏加载黑屏问题

//设置透明Theme,防止黑白屏,进入app闪烁

1
2
3
4
<style name="Theme.AppTranslucent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
</style>

6,物理后退模拟点击Home键

后退,直接返回桌面,并不退出应用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@Override
public void onBackPressed() {
//super.onBackPressed();这句话要注掉,否则又去调用默认的back处理方式了
Intent mIntent = new Intent();
mIntent.setAction(Intent.ACTION_MAIN);
mIntent.addCategory(Intent.CATEGORY_HOME);
startActivity(mIntent);
}
``` java
### 7,根据android文件ID,查找缩略图
视频缩略图类似,通过多媒体数据库获取。
``` java
private Bitmap getImageThumbBitmap(int ImageID, String path) {
Bitmap bitmap = null;
if (ImageID!=-1&&ImageID>0) {
bitmap = MediaStore.Images.Thumbnails.getThumbnail(
contentResolver, ImageID, MediaStore.Images.Thumbnails.MINI_KIND, null);
}
if (bitmap == null){
bitmap = MyFileUtils.getImageThumbnail(path, 80, 80);
}
return bitmap;
}

8,string.xml中不能有特殊符号

项目中要在string.xml 中显示特殊符号,如@号冒号等,直接写肯定不行啦。使用ASCII码进行显示。如:

1
2
3
@号 &#064;
:号 &#058;
空格 &#160;

9,Android自定义通知栏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void createNotification() {
String title = context.getString(R.string.notify_downloading_title)+fileName;
notification = new Notification(
R.mipmap.ic_launcher,//应用的图标
title,
System.currentTimeMillis());
notification.tickerText = title;
//notification.flags = Notification.FLAG_ONGOING_EVENT;
notification.flags = Notification.FLAG_AUTO_CANCEL;
contentView = new RemoteViews(context.getPackageName(),R.layout.notification_item);
contentView.setTextViewText(R.id.notificationTitle, title);
contentView.setTextViewText(R.id.notificationPercent, "0%");
contentView.setProgressBar(R.id.notificationProgress, 100, 0, false);
notification.contentView = contentView;
//点击后进入缓存列表
downloadIntent = new Intent(context, HomeActivity.class);
downloadIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(context, 0, downloadIntent, 0);
notification.contentIntent = pendingIntent;
notificationManager = NotificationUtils.getManager(context);
}

10,Android常用工具,其他相关

IDE:android studio,eclipse
文本编辑器:Sublmine,Atom
版本控制:svn,git
笔记:为知笔记,有道云笔记,万象笔记
开发者学习常用站点:github,csdn,简书,掘金,推酷,慕课网
android大牛:一大堆,希望有你。

欢迎交流,未完待续!

更多精彩,访问Dusan’s blog :duqian.site

1
Dusan_杜乾:Q:291902259,公众号:OpenDeveloper